package com.xinziruo.richclient;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class TaobaoMobileTest {
	
	//分类
    static String  cat="CJFQ";
    //品牌
    static Map<String,String> brands = initBrands();
    static String pidvid="20000:11542";
    static String host = "http://spu.taobao.com/spu/3c/";
    public static void main(String[] args) {
    	
    	//列表url
    	//http://spu.taobao.com/spu/3c/spulist.htm?cat=1512&pidvid=20000:11542
    	String listUrl = "http://spu.taobao.com/spu/3c/spulist.htm?cat=1512&pidvid=20000:11542";
    	Document doc = getDoc(listUrl);
    	if(null != doc){
    		Elements links = doc.select("div.choices-item > ul > li");
    		for(Element link : links){
    			Integer price = 0;
    			String productUrl  = host + link.select("p.general > a").attr("href");
    			productUrl = productUrl.replaceAll("##content", "");
    			String loc = productUrl;
    			String spuid = productUrl.replaceAll(".*spuid=(.*)#content", "$1");
    			//http://spu.taobao.com/json/commentlist.htm?_input_charset=utf-8&spuid=214826097&cat=1512&pageSize=1&cp=1
    			String commentUrl = "http://spu.taobao.com/json/commentlist.htm?_input_charset=utf-8&cat=1512&pageSize=1&cp=1&spuid="+spuid;
    			String buyerDiscuss = 	getComment(commentUrl); 			
    			Document product = getDoc(productUrl);
    			if(null == product){
    				System.out.println("抓取商品失败");
    			}else{
    				getProduct(product);
    			}
    			System.out.println("loc:"+loc);
    			System.out.println("buyerDiscuss:"+buyerDiscuss);
    			//<p><font>约</font><span>&yen;<em>327</em></span>
    			try{
    				price = Integer.valueOf(link.select("div.title p:eq(1) > span > em").text());
    			}catch(Exception e){
    				
    			}
    			System.out.println("price:"+price);
    		}
    	}

    }
    
    public static String getComment(String commentUrl) {
    	StringBuffer buyerDiscuss = new StringBuffer();
    	Document doc = getDoc2(commentUrl);
    	String json = "";
    	if(null != doc){
	    	json = doc.select("body").text().replaceAll("jsoncallback\\((.*)\\)", "$1");
	    	/*
	    	 */
	    	try{
	    		JSONObject jsonObject = new JSONObject(json);
		    	JSONArray expressionwords= jsonObject.getJSONArray("expressionword");
		    	for(int i=0;i<expressionwords.length();i++){
		    		JSONObject comment = expressionwords.getJSONObject(i);
		    		buyerDiscuss.append(comment.get("alias")+"("+comment.get("count")+ ")"+";");
		    	}
		    	return buyerDiscuss.toString();
	    	}catch(JSONException e){
	    		System.out.println("解析json【"+json+"】失败！");
	    		return "";
	    	}
	    	
    	}
    	return "";
	}

	public static Document getDoc(String url) {
    	Connection conn = Jsoup.connect(url);
		conn.method(Method.GET);
    	conn.followRedirects(false);
    	conn.timeout(5000);
    	try {
			Document doc = conn.get();
			return doc;
		}catch(Exception e){
			System.out.println("访问url【"+url+"】时出错，无法继续");
			return null;
		}
    }
	
	public static Document getDoc2(String url) {
    	Connection conn = Jsoup.connect(url);
    	conn.ignoreContentType(true);
		conn.method(Method.GET);
    	conn.followRedirects(false);
    	conn.timeout(5000);
    	try {
			Document doc = conn.get();
			return doc;
		}catch(Exception e){
			System.out.println("访问url【"+url+"】时出错，无法继续");
			return null;
		}
    }
    
    public static void getProduct(Document doc) {
    	/*
    	<str name="moreImages">http://img.taobaocdn.com/bao/uploaded/i4/T1g3bQXeVfXXXpYYZ5_055130.jpg_220x220.jpg</str>
		<str name="buyerDiscuss">性能不错(781);配件很好(355);</str>
		<str name="loc">http://spu.taobao.com/spu/3c/detail.htm?cat=1512&spuid=208961737</str>
		<str name="image">http://img.taobaocdn.com/bao/uploaded/i4/T1g3bQXeVfXXXpYYZ5_055130.jpg_220x220.jpg</str>
		<str name="brand">三星</str>
		<str name="descr">三星型号;GALAXYNoteII;</str>
		<str name="props">网络类型:;GSM/WCDMA/LTE;操作系统:;ANDROID;</str>
		<str name="tcon">商务;智能;看电影</str>
		<double name="price">3350.0</double>
		<str name="oldprice">3350</str>
		<str name="id">7rQBba</str>
		<str name="classify">手机大全</str>
		<str name="project">com.xinziruo.mobile</str>
		<int name="idAsHash">1657544177</int>
		<str name="tags">samsung;三星;galaxy;note</str>
		<int name="randomRegion">5022</int>
		<long name="createTime">1366188845093</long>
		<int name="dataFlag">1</int>
    	 */
    	StringBuffer images = new StringBuffer();
    	for(Element li : doc.select("li.tiny-item")){
    		images.append(li.select("a > img").attr("src")+";");
    	}
    	String moreImages = images.toString();
    	//*****************************************
    	String buyerDiscuss = "";
    	String image = moreImages.split(";")[0];
    	String brand = brands.get("11542");
    	//*****************************************
    	StringBuffer descrs = new StringBuffer();
    	for(Element li : doc.select("div#param > table > tbody > tr > td")){
    		if(!StringUtils.isBlank(li.text().replaceAll("\n|\\s", ""))){
    			descrs.append(li.text().replaceAll("\n|\\s", "")+";");
  			}
    	}
    	String descr = descrs.toString();
    	//*****************************************
    	StringBuffer propss = new StringBuffer();
    	for(Element li : doc.select("div.idec-spu-info > ul >li")){
    		if(!StringUtils.isBlank(li.text().replaceAll("\n|\\s", ""))){
    			propss.append(li.text().replaceAll("\n|\\s", "")+";");
  			}
    	}
    	String props = propss.toString();
    	//***************************************
    	StringBuffer tcons = new StringBuffer();
    	for(Element li : doc.select("dl.phone_feature:eq(0) > dd > a")){
    		if(!StringUtils.isBlank(li.text().replaceAll("\n|\\s", ""))){
    			tcons.append(li.text().replaceAll("\n|\\s", "")+";");
  			}
    	}
    	String tcon = tcons.toString();
    	String title = doc.select("h1").attr("title");
    	System.out.println("------------------------------------------------------");
    	System.out.println("moreImages:"+moreImages);
    	System.out.println("image:"+image);
    	System.out.println("buyerDiscuss:"+buyerDiscuss);
    	System.out.println("brand:"+brand);
    	System.out.println("descr:"+descr);
    	System.out.println("props:"+props);
    	System.out.println("tcon:"+tcon);
    	System.out.println("title:"+title);
    	
    }
    
    
    public static Map<String,String> initBrands(){
    	Map<String,String> brands = new HashMap<String,String>();
    	brands.put("30111","苹果");
		brands.put("81156","三星");
		brands.put("3506680","小米");
		brands.put("33564","诺基亚");
		brands.put("11813","华为");
		brands.put("11119","联想");
		brands.put("3261618","HTC");
		brands.put("10752","索尼");
		brands.put("11208","中兴");
		brands.put("28247","OPPO");
		brands.put("11542","酷派");
		brands.put("10123","摩托罗拉");
		brands.put("47416","黑莓");
		brands.put("25143","魅族");
		brands.put("10428","LG");
		brands.put("84416","天语");
		brands.put("91621","vivo");
		brands.put("11531","金立");
		brands.put("11454","大显");
		brands.put("10209","索尼爱立信");
		brands.put("10246","飞利浦");
		brands.put("30641","长虹");
		brands.put("27726","步步高");
		brands.put("11548","天时达");
		brands.put("4110526","优思");
		brands.put("10590","夏普");
		brands.put("11599","海信");
		brands.put("11659","紫光电子");
		brands.put("16732010","ThL");
		brands.put("44443225","基伍");
		brands.put("10858","TCL");
		brands.put("44246390","优尔得");
		brands.put("10681","泛泰");
		brands.put("11161","康佳");
		brands.put("3279905","贝尔丰");
		brands.put("95986","亿通");
		brands.put("26487995","朵唯");
		brands.put("96761","高新奇");
		brands.put("10779","夏新");
		brands.put("10955","波导");
		brands.put("123840","德赛");
		brands.put("47542667","Vinus");
		brands.put("16168852","E·Xun");
		brands.put("11016","海尔");
		brands.put("21210","奥克斯");
		brands.put("60679","纽曼");
		brands.put("11263","东信");
		brands.put("36077726","VOTO");
		brands.put("3278450","锋达通");
		brands.put("105216","七喜");
		brands.put("11057","中电华通");
		brands.put("10905","首信");
		brands.put("117163","中恒");
		brands.put("9658574","尼彩");
		brands.put("35167","卓普");
		brands.put("11544887","聆韵");
		brands.put("28606","奔迈");
		brands.put("10374","萨基姆");
		brands.put("123843","广信");
		brands.put("123851","GNET");
		brands.put("33549","多普达");
		brands.put("11653","万利达");
		brands.put("112282","知己");
		brands.put("95989","GT");
		brands.put("11207","英华OK");
		brands.put("123837","邦华");
		brands.put("20796","爱国者");
		brands.put("41993221","酷比");
		brands.put("266916416","尼凯恩");
		brands.put("30654","荣事达");
		brands.put("104029268","语信");
		brands.put("4031620","KPT");
		brands.put("44715721","MANN");
		brands.put("11656","华硕");
		brands.put("11841","创维");
		brands.put("21929","现代");
		brands.put("11372","UT斯达康");
		brands.put("27725","高科");
		brands.put("123850","普莱达");
		brands.put("123838","宝捷讯");
		return brands;
    }
    
    //移除集合中空的值
    private List<String> removeEmptyValue(List<String> fieldValueAsStringList) {
  		List<String> temps = new ArrayList<String> ();
  		if(fieldValueAsStringList.size() <= 0){
  			return Collections.emptyList();
  		}
  		for(String temp : fieldValueAsStringList){
  			if(!StringUtils.isBlank(temp.replaceAll("\n|\\s", ""))){
  				temps.add(temp.replaceAll("\n|\\s", ""));
  			}
  		}
  		return temps;				
  	}
    
}